Quick Index
Enum Format Conventions


Enum formatting conventions:


You will see this exemplified in the examples that follow.

Example 1 -- Enums vs No-Enums


On the left we do NOT use enums. What is 1? What is 2? This code is not clear.

On the right we do YES use enums. This is code is much clearer -- the constants now have names, WINTER, SPRING, etc.

Under the hood, the enum is a constant -- it is basically a descriptive wrapper on an constant.
public class UserProfile {
	private String name;
	private int favoriteSeason;

	private void printSummary()  {
		prn("Name: " + this.name);
		switch(this.favoriteSeason) {
			case 1:
				prn("My favorite season is winter!");
				break;
			case 2:
				prn("My favorite season is spring!");
				break;
			case 3:
				prn("My favorite season is summer!");
				break;
			case 4:
				prn("My favorite season is fall!");
				break;
		}
	}

	//etc (more code follows)

}
public class UserProfile {
	public enum Season {WINTER, SPRING, SUMMER, FALL};
	private String name;
	private Season favoriteSeason;

	private void printSummary()  {
		prn("Name: " + this.name);
		switch(this.favoriteSeason) {
			case WINTER:
				prn("My favorite season is winter!");
				break;
			case SPRING:
				prn("My favorite season is spring!");
				break;
			case SUMMER:
				prn("My favorite season is summer!");
				break;
			case FALL:
				prn("My favorite season is fall!");
				break;
		}
	}

	//etc (more code follows)

}
Download Example 1


Example 1.

Example 2 -- Enums vs No-Enums


On the left we do NOT use enums. In the constructor call we are setting difficulty as "3"? Is three high or low difficulty? What is the range of integers? This code is not clear.

On the right we do YES use enums. This is code is much clearer -- the constants now have names: EASY, MEDIUM, HARD.
public class Game {
	private String name;
	private int difficultyLevel;

	public static void main(String[] args)  {
		Game game = new Game("My Game", 3);
		System.out.println(game.toString());
	}

	//etc (more code follows)

}
public class Game {
	public enum DifficultyLevel {EASY, MEDIUM, HARD};
	private String name;
	private DifficultyLevel level;

	public static void main(String[] args)  {
		Game game = new Game("My Game", DifficultyLevel.HARD);
		System.out.println(game.toString());
	}

	//etc (more code follows)

}
Download Example 2


Example 2.